const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    productlist:[],
    thispNum: '',
    pageEnd: false,
    bottom_tap: false,
    name:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.func.onPageLoad(this, options);
        this.setData({
           name:options.name
        });
  },

    collectChanges(e) {
        let data = e.detail;
        this.setData({
            [`productlist[${data.index}].add_cart`]: data.value
        });
    },

  checkDataLength(value){
     if(value ===0){
         app.func.confirm('提示','抱歉，没有找到相关商品', false)
             .then(()=>{
                wx.navigateBack({
                    delta: 1
                });
             })
     }
  },

  onShow:function(){
    app.func.postPromise('/product/search/0?length=20',{ name:this.data.name })
        .then(([code,res])=>{
            this.checkDataLength(res.data.length);

            this.setData({
              productlist:res.data,
              thispNum: res.data.length,
            });
        })
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.pageEnd) {
      this.setData({  bottom_tap: true });
    } else {
      wx.showLoading({  title: '加载更多', mask: true });
      var thispNum = this.data.thispNum;
      var name = this.data.name;
      var that = this;
      app.func.postPromise('/product/search/'+ thispNum +'?length=20',{name: name})
          .then(([code,res])=>{
            if (res.data.length == 0) {
              that.setData({
                pageEnd: true,
                bottom_tap: true
              }, ()=> wx.hideLoading())
            } else {
              var tempArray = that.data.productlist;
              tempArray = tempArray.concat(res.data);
              that.setData({
                productlist: tempArray,
                thispNum: that.data.thispNum + res.data.length
              }, ()=> wx.hideLoading());
            }
          })
    }
  },
  login:function () {
    wx.navigateTo({
      url:'/pages/auth/auth'
    })
  },


})
